<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script>
    // function compressString(str) {
    //   let comStr = "";
    //   let count = 1;

    //   for (let i = 0; i < str.length; i++) {
    //     if (str[i] === str[i + 1]) {
    //       count++;
    //     } else {
    //       comStr += str[i] + count;
    //       count = 1;
    //     }
    //   }

    //   return str.length <= comStr.length ? str : comStr;
    // }
    function compressString(str) {
      let comStr = '';
      let count = 1;

      for (let i = 0; i < str.length; i++) {
        if (str[i] === str[i + 1]) {
          count++;
        } else {
          comStr += str[i] + count;
          count = 1;
        }
      }

      return comStr.length > str.length ? str : comStr;
    }

    console.log(compressString('aabccccaaa'));
    console.log(compressString('abcedfg'));

    function throttle(func, wait) {
      let timeStamp = 0;

      return function (...args) {
        const context = this;

        const start = Date.now();
        const eTime = start - timeStamp;

        if (eTime >= wait) {
          func.apply(context, args);
          timeStamp = start;
        }
      };
    }
  </script>
  <body></body>
</html>
